<template>
  <el-row>
    <el-col :span="24" class="bg1">
      <div class="grid-content bg-purple">
        <FormPanel ref="formPanel" :list="formList" :cols="4" :isSubmitBtn="false" @formChange="changeHandle" />
      </div>
      <div class="btn">
        <el-button size="small" icon="el-icon-search" type="primary" @click="query">查询</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" @click="reset">重置</el-button>
      </div>
    </el-col>
    <el-col :span="24" class="bg2">
      <span>车型车系</span>
      <FilterTable
        ref="table"
        :columns="columns"
        :datakey="'rows'"
        columnsRef="yxqAddcsopeTable"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :isColumnFilter="false"
        :isToperInfo="false"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      >
      </FilterTable>
    </el-col>
    <el-col :span="24" style="margin-top:10px;">
      <div class="btn">
        <el-button size="small" icon="el-icon-folder-add" type="primary" class="btnStyle" @click="save">保存</el-button>
        <el-button size="small" icon="el-icon-refresh-left" type="primary" class="btnStyle" @click="saveReset">取消</el-button>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getaddActcsopeList, getBrandsList, getSeriessList } from '@/api/repair/repair/activitesManage';
import { getXmcxzSelect } from '@/api/repair/repairReception/customer';
import { getChexSelect } from '@/api/repair/repairReception/customer';
// import Newaddpjx from './addActivity';
export default {
  name: 'addActcsope',
  components: {
    //  AddPjxm
  },
  mixins: [dictionary],
  data() {
    return {
      columns: this.createTableColumns(),
      formList: this.createFormList(),
      visible: false,
      fetchParams: { xhrAbort: false },
      fetchApi: getaddActcsopeList,
      onChangeColumn: []
    };
  },
  mounted() {
    this.getBrandsList();
  },
  methods: {
    validateFn(rule, value, callback) {
      if (value.length < rule.limit) {
        return callback(new Error(rule.message));
      }
      callback();
    },
    // 车系下拉框
    async getSeries(id) {
      const res = await getSeriessList(id);
      console.log('11111111111111', res);
      this.formList.find(x => x.fieldName === 'modelLabourCode').itemList = res.data.map(x => ({ value: x.SERIES_CODE, text: x.SERIES_NAME }));
    },

    async getBrandsList() {
      const res = await getBrandsList();
      this.formList.find(x => x.fieldName === 'brandCode').itemList = res.data.map(x => ({ text: x.BRAND_NAME, value: x.BRAND_CODE }));
    },
    // 品牌下拉框改变时
    changePinpSelect(id) {
      this.clearFormItem(this.formList.find(x => x.fieldName === 'modelLabourCode'));
      if (!id) return;
      // 获取车系
      this.getSeries(id);
    },

    // 清空下拉框表当控件的值和下拉列表
    clearFormItem(item) {
      item.initialValue = undefined;
      item.itemList = [];
    },
    // async getSeriessList(val) {
    //   const res = await getSeriessList(val);
    //   this.formList.find(x => x.fieldName === 'seriesCode').initialValue = '';
    //   this.formList.find(x => x.fieldName === 'seriesCode').itemList = res.data.map(x => ({ value: x.SERIES_CODE, text: x.SERIES_NAME }));
    // },
    save() {
      if (this.onChangeColumn.length == 0) {
        this.$notify.error({
          title: '错误',
          message: '请选择一条内容'
        });
      } else {
        this.$emit('closeaddAct', this.onChangeColumn);
      }
    },
    saveReset() {
      this.$emit('closeaddAct', false);
    },
    query() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    reset() {
      this.$refs.formPanel.RESET_FORM();
      this.changeHandle();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { offset: 0, xhrAbort: false });
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandCode',
          itemList: [],
          placeholder: '请选择...',
          initialValue: '',
          change: val => {
            this.changePinpSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'modelLabourCode',
          itemList: [],
          placeholder: '请选择...',
          initialValue: ''
        }
        //  {
        //     type: 'SELECT',
        //     label: '车型',
        //     fieldName: 'models',
        //     itemList:[],
        //     placeholder: '请选择',
        //     initialValue: '',
        // },
        // {
        //     type: 'SELECT',
        //     label: '车辆配置',
        //     fieldName: 'configCodes',
        //     itemList:[],
        //     placeholder: '请选择',
        //     initialValue: '',
        // },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '品牌',
          dataIndex: 'BRAND_NAME1',
          sorter: true
        },
        {
          title: '车系',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME1',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '车辆配置',
          dataIndex: 'CONFIG_NAME',
          sorter: true,
          showOverflowTooltip: true
        }
      ];
    }
  }
};
</script>

<style scoped lang="less">
.bg1 {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  padding-left: 0;
  padding-top: 15px;
  padding-bottom: 15px;
}
.bg2 {
  margin-top: 10px;
  margin-bottom: 10px;
}
.btn {
  text-align: center;
}
.btnStyle {
  margin-top: 10px;
}
</style>
